package com.ding.eduservice.controller;


import com.ding.commonutils.Result;
import com.ding.eduservice.client.VodClient;
import com.ding.eduservice.entity.EduVideo;
import com.ding.eduservice.service.EduChapterService;
import com.ding.eduservice.service.EduVideoService;
import com.ding.servicebase.exceptionmanage.Dingexception;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-16
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
   @PostMapping("addVideo")
   public Result addVideo(@RequestBody EduVideo eduVideo){
         eduVideoService.addvideodata(eduVideo);
         return Result.ok();
   }
    //删除小节
   @DeleteMapping("delVideo/{videoId}")
    public Result delVideo(@PathVariable String videoId){
       //根据小节id获取视频id再调用方法
       EduVideo byId = eduVideoService.getById(videoId);
       String videoSourceId = byId.getVideoSourceId();
       if(!StringUtils.isEmpty(videoSourceId)){
           Result result = vodClient.delVideo(videoSourceId);
           if(result.getCode() == 20001){
               throw new Dingexception(20001,"删除视频失败了,熔断器。。");
           }
       }

       //删除小节
       eduVideoService.delvideodata(videoId);
       return Result.ok();
   }

    //修改小节
    @PostMapping("updateVideo")
    public Result updateVideo(@RequestBody EduVideo eduVideo){
       eduVideoService.updatevideodata(eduVideo);
       return Result.ok();
    }

    //根据id查询video信息
    @GetMapping("getById/{videoId}")
    public Result getById(@PathVariable String videoId){
       EduVideo eduVideo = eduVideoService.getInfoById(videoId);
       return Result.ok().data("eduVideo",eduVideo);
    }

}

